<?php

class Tyh_Config
{

	private static $instance;
	public $tyh_debug = FALSE;

	protected function __construct()
	{
		// include if file exists
		if( !file_exists( $filename = TYH_APPPATH . 'config/config.php' ) )
		{
			wp_die( _x( 'Config file could not be loaded. Missing file: ', TYH_LANG_CONTEXT ) . $filename . '.' );
		}
		// get the config file...
		require_once($filename);

		// convert config obj to properties of this (Tyh_Config) class
		$this->setProperties( $config );
	}

	protected function setProperties( $config )
	{
		if( !is_object( $config ) )
			return FALSE;

		foreach ( $config as $property => $value ) {
			$this->set( $property, $value );
		}
	}

	public function set( $property, $value )
	{
		$property = trim( $property );

		if( !is_string( $property ) AND empty( $property ) )
		{
			return FALSE;
		}
		$this->$property = $value;

		// make it chainable for example: 
		// $config->set('property', 'value')->set('property', 'value');
		return self::$instance;
	}

	public function get( $property )
	{
		if( property_exists( $this, $property ) )
		{
			return $this->$property;
		} else
		{
			if( WP_DEBUG && $this->tyh_debug )
			{
				trigger_error( _x( 'Property does not exist. Property: "', TYH_LANG_CONTEXT ) . $property . '"' );
			}

			return NULL;
		}
	}

	/*
	 * Removes a property
	 */

	public function remove( $property )
	{
		if( property_exists( $this, $property ) )
		{
			unset( $this->$property );

			return TRUE;
		} else
		{
			if( WP_DEBUG && $this->tyh_debug )
			{
				trigger_error( _x( 'Property does not exist. Property: "', TYH_LANG_CONTEXT ) . $property . '"' );
			}

			return FALSE;
		}
	}

	// TODO: needs some work, what work? Include the file, add the new config properties to the config
	public function load( $filename, $ext = 'php' )
	{
		$file = TYH_APPPATH . 'config/' . $filename . '.' . $ext;

		if( $file_exists = file_exists( $file ) )
		{
			require_once($file);

			$this->setProperties( $config );
		}

		return $file_exists;
	}

	public static function getInstance()
	{
		if( !isset( self::$instance ) )
		{
			self::$instance = new self();
		}

		return self::$instance;
	}

}